package com.dcstudy.redis.controller;

import cn.hutool.core.util.IdUtil;
import com.dcstudy.redis.entities.User;
import com.dcstudy.redis.entities.UserDTO;
import com.dcstudy.redis.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @author lichenyang
 * @create 2021-05-18 10:15 上午
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api("用户模块")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation("新增数据")
    @PostMapping("/add")
    public void add() {
        for (int i = 1; i < 5; i++) {
            User user = new User();
            user.setUsername("test" + i);
            user.setPassword(IdUtil.simpleUUID().substring(0, 6));
            user.setSex((byte) new Random().nextInt(2));
            userService.add(user);
        }
    }

    @ApiOperation("删除数据")
    @PostMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        userService.delete(id);
    }

    @ApiOperation("修改数据")
    @PostMapping("/update")
    public void update(@RequestBody UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        userService.update(user);
    }

    @ApiOperation("查询数据")
    @PostMapping("/select/{id}")
    public User select(@PathVariable Integer id) {
        return userService.select(id);
    }

}
